package com.longrise.android.byjk.wxapi;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.app.BYJKAppLike;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.common.eventbean.PayEventBean;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;


import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler, OnClickListener {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_wxpay);

        // api = WXAPIFactory.createWXAPI(this, UserInfor.getInstance().APP_ID);
        api = BYJKAppLike.mIWXAPI;
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }


    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (resp.errCode) {
                case 0:
                    dealPayResult(true);
                    break;
                case -1:
                    dealPayResult(false);
                    break;
                case -2:
                    dealPayResult(false);
                    break;

                default:
                    break;
            }

        }
    }

    /**
     * 通知其他页面 刷新
     *
     * @param isSuccess
     */
    private void dealPayResult(boolean isSuccess) {
        PayEventBean bean = new PayEventBean();
        bean.setWxPaySuccess(isSuccess);
        EventBus.getDefault().post(bean);

        if (isSuccess) {
            finish();
            overridePendingTransition(0, 0);

//            mTvcontent.setText("支付成功");
//            mIv.setImageResource(R.drawable.icon_right);
        } else {
            View mDialogview = View.inflate(WXPayEntryActivity.this, R.layout.dialog_courses_evaluate, null);
            TextView mTvtitle = (TextView) mDialogview.findViewById(R.id.dialog_courses_title);
            mTvtitle.setText("微信支付");
            ImageView mIv = (ImageView) mDialogview.findViewById(R.id.dialog_share_success_iv);
            TextView mTvcontent = (TextView) mDialogview.findViewById(R.id.dialog_courses_content);
            TextView mTvbt = (TextView) mDialogview.findViewById(R.id.dialog_courses_evaluate_bt);
            mTvbt.setOnClickListener(this);
            mTvcontent.setText("支付失败");
            mIv.setImageResource(R.drawable.icon_tips);
            Dialog dialog = DialogUtil.getInstance().creatAlertDialog(WXPayEntryActivity.this, mDialogview, 266, 195);
            dialog.setCancelable(false);
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_courses_evaluate_bt:
                DialogUtil.getInstance().dismiss();
                finish();
                overridePendingTransition(0, 0);
                break;

            default:
                break;
        }

    }

}